package com.dqp.cslggroup.School;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import com.dqp.cslggroup.ConnectSever.Accept;
import com.dqp.cslggroup.EventBus.MessageEvent;
import com.dqp.cslggroup.Features.GlideImageLoader;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.SchoolMaps;
import com.dqp.cslggroup.UI.BaseActivity;
import com.dqp.cslggroup.Util.QQ;
import com.dqp.cslggroup.Util.ToastUtil;
import com.dqp.cslggroup.Util.UiUtils;
import com.dqp.cslggroup.ZCJXJ.XSGZ_login;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;


public class School extends BaseActivity {
    private Banner banner;
    private ArrayList<String> list_path = new ArrayList<>();
    private ArrayList<String> list_title = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.school);
        EventBus.getDefault().register(this);
        banner = findViewById(R.id.banner);
        getPath();
    }

    @SuppressLint("HandlerLeak")
    public void getPath() {
        new Thread(() -> {
            ArrayList<com.dqp.cslggroup.bean.Club> dataList = Accept.schoolImg();
            for (int i = 0; i < dataList.size(); i++) {
                com.dqp.cslggroup.bean.Club club = dataList.get(i);
                list_path.add(club.getClub_img());
                list_title.add(club.getClub_name());
            }
            EventBus.getDefault().post(new MessageEvent("School", 1, null));
        }).start();
    }


    private void startBanner(List imageUrl, List<String> titles) {
        //设置banner样式(显示圆形指示器)
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置指示器位置（指示器居右）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(imageUrl);
        //设置banner动画效果
        //banner.setBannerAnimation(Transformer.CubeIn);
        //设置标题集合（当banner样式有显示title时）
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);//设置页码与标题
        banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(2000);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }


    public void startWeibo(String id, String url) {
        if (QQ.isWeiBoClientAvailable(School.this)) {
            Intent intent = new Intent();
            ComponentName cmp = new ComponentName("com.sina.weibo", "com.sina.weibo.page.ProfileInfoActivity");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(cmp);
            intent.putExtra("uid", id);
            startActivity(intent);
        } else {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    }


    public void SchoolonClick(View v) {
        switch (v.getId()) {
            case R.id.school_map:
                UiUtils.startActivity(School.this, SchoolMaps.class);
                break;

            case R.id.school_web:
                UiUtils.openBrowser(School.this, "http://www.cslg.cn/");
                break;

            case R.id.school_club:
                UiUtils.startActivity(School.this, Club.class);
                break;

            case R.id.school_weibo:
                startWeibo("5137439436", "https://weibo.com/u/5137439436");
                break;

            case R.id.school_College:
                UiUtils.startActivity(School.this, College.class);
                break;

            case R.id.school_ip:
                UiUtils.startActivity(School.this, DormitoryIP.class);
                break;

            case R.id.school_calendar:
                UiUtils.startActivity(School.this, Schoolcalendar.class);
                break;

            case R.id.school_news:
                UiUtils.openBrowser(School.this, "http://news.cslg.edu.cn/");
                break;

            case R.id.school_video:
                UiUtils.openBrowser(School.this, "http://news.cslg.edu.cn/zsyf1314.mp4");
                break;

            case R.id.pj:
                UiUtils.openBrowser(School.this, "http://zhpj.cslg.edu.cn/jsjxpj/");
                ToastUtil.showLongToast("密码为出生年月日！");
                break;

            case R.id.CampusTalk:
                UiUtils.startActivity(School.this, CampusTalk.class);
                break;

            case R.id.zc:
                UiUtils.startActivity(School.this, XSGZ_login.class);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            super.onDestroy();
            this.finish();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        if (event.getEvent().equals("School")) {
            if (event.getWhat() == 1) {
                startBanner(list_path, list_title);
            }
        }
    }
}
